package com.hrsoft.edi.stub;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.hrsoft.edi.stub.consant.AppInfo;
import com.hrsoft.edi.stub.request.EntryorderCreateRequest;
import com.qimen.api.DefaultQimenClient;
import com.qimen.api.QimenClient;
import com.qimen.api.response.EntryorderCreateResponse;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zxf
 * @date 2019/8/6 13:33
 */
@Slf4j
public class EntryOrderCreateTest
{

    public static void create()
        throws ApiException
    {
        QimenClient client = new DefaultQimenClient(AppInfo.URL, AppInfo.APP_KEY, AppInfo.SECRET);
        EntryorderCreateRequest createRequest = new EntryorderCreateRequest();
        createRequest.setCustomerId(AppInfo.CUSTOMER_ID);
        createRequest.setVersion("2.0");
        EntryorderCreateRequest.EntryOrder entryOrder = new EntryorderCreateRequest.EntryOrder();
        entryOrder.setWarehouseCode(AppInfo.WAREHOUSE_CODE);
        entryOrder.setOwnerCode(AppInfo.OWNER_CODE);
        entryOrder.setLogisticsCode("SF");
        entryOrder.setEntryOrderCode("2A" + RandomUtil.randomNumbers(15));
        entryOrder.setExpectEndTime(DateUtil.formatDateTime(new Date()));
        entryOrder.setExpectStartTime(DateUtil.formatDateTime(new Date()));
        entryOrder.setOperateTime(DateUtil.formatDateTime(new Date()));
        entryOrder.setBusinessId("dssdasdas");
        entryOrder.setPurchaseOrderCode("2A" + RandomUtil.randomNumbers(15));
        entryOrder.setOrderType("CGRK");
        EntryorderCreateRequest.SenderInfo senderInfo = new EntryorderCreateRequest.SenderInfo();
        senderInfo.setName("胡胡胡");
        senderInfo.setMobile("13887898786");
        senderInfo.setProvince("浙江省");
        senderInfo.setCity("杭州市");
        senderInfo.setDetailAddress("西湖大街");
        EntryorderCreateRequest.ReceiverInfo receiverInfo = new EntryorderCreateRequest.ReceiverInfo();
        receiverInfo.setName("马云");
        receiverInfo.setMobile("13887898786");
        receiverInfo.setProvince("浙江省");
        receiverInfo.setCity("杭州市");
        receiverInfo.setDetailAddress("西湖大街");
        entryOrder.setReceiverInfo(receiverInfo);
        entryOrder.setSenderInfo(senderInfo);
        createRequest.setEntryOrder(entryOrder);
        EntryorderCreateRequest.RelatedOrder relatedOrder = new EntryorderCreateRequest.RelatedOrder();
        relatedOrder.setOrderCode("111");
        relatedOrder.setOrderType("CG");
        List<EntryorderCreateRequest.RelatedOrder> list = new ArrayList<>();
        list.add(relatedOrder);
        entryOrder.setRelatedOrders(list);
        List<EntryorderCreateRequest.OrderLine> orderLines = new ArrayList<>();

        for (int i = 1; i <= 1; i++)
        {
//            String key = StringUtils.leftPad(""+i,4,"0");
            EntryorderCreateRequest.OrderLine orderLine = new EntryorderCreateRequest.OrderLine();
            orderLine.setItemCode("YYZYA");
            orderLine.setPlanQty("100");
            orderLine.setActualPrice("1");
            orderLine.setInventoryType("ZP");
            orderLine.setOwnerCode(AppInfo.OWNER_CODE);
            orderLines.add(orderLine);
        }
        createRequest.setOrderLines(orderLines);
        long time1 = System.currentTimeMillis();
        EntryorderCreateResponse response = client.execute(createRequest);
        long time2 = System.currentTimeMillis();
        log.info("响应:{} ERP订单编码:{} wms仓储编码:{} 耗时:{}",
            response.getFlag(),
            entryOrder.getEntryOrderCode(),
            response.getEntryOrderId(),
            time2 - time1);
    }

    public static void main(String[] args)

        throws ApiException
    {
//        create();
        for (int i = 0; i < 1; i++)
        {
            create();
        }
    }

}
